#include "state_intro.h"
#include "krryn.h"

using namespace krryn::math;
using namespace krryn::system;
using namespace krryn::system_imp;
using namespace krryn::usr;
using namespace krryn::core;
using namespace krryn::scene;
using namespace krryn::view;
using namespace krryn::gui;

static state_intro _state_intro;

state_intro::~state_intro(){}

void state_intro::init(){
	// generic stuff that you always have to do
	system_info i;

	m_Manager = new job_manager(i.number_of_processors() - 1);
	m_Default = new default_renderer<api>(m_Manager);
	m_Camera  = new camera_fps();

	m_VB      = new vertex_buffer<api>(*m_Default->m_Renderer, 4);
	m_IB	  = new index_buffer<api>(*m_Default->m_Renderer, 6);

	{
		hardware_locker l_VertexRiaa(*m_VB, write);
		hardware_locker l_IndexRiaa(*m_IB, write);

		//*m_VB += vertex( 1, 1, 0).color(color3(1, 0, 0));
		//*m_IB += 0;

		//*m_VB += vertex( 1, 0, 0).color(color3(0, 0, 1));
		//*m_IB += 1;

		//*m_VB += vertex(-1,-1, 0).color(color3(0, 1, 0));
		//*m_IB += 2;

		*m_VB += vertex(-1,-1, 0).color(color3(0, 1, 0));
		*m_VB += vertex( 1, 0, 0).color(color3(0, 0, 1));
		*m_VB += vertex( 1, 1, 0).color(color3(1, 0, 0));
		*m_VB += vertex( 1,-1, 0).color(color3(1, 0, 0));

		*m_IB += 0;
		*m_IB += 1;
		*m_IB += 2;
		*m_IB += 0;
		*m_IB += 1;
		*m_IB += 3;
	}
}

void state_intro::activate(){}

void state_intro::deactivate(){}

void state_intro::tick(float a_DeltaTime){
	m_Default->m_Renderer->begin_scene(); // RAII this?

	{
		hardware_locker l_VertexRiaa(*m_VB, read);
		hardware_locker l_IndexRiaa(*m_IB, read);

		m_VB->draw_indexed(*m_IB);
	}

	//scene::mesh m;
	//math::matrix t;

	//static float f = 0;
	//f += 0.5f;
	//t.rotate_z(f);

	//m_Default->m_Renderer->draw(m, t);

	m_Default->m_Renderer->end_scene();

	m_EventLoop.update(); // we would rather not have this here! And this?
}
